/**
 * 质控病种列表初始化
 */
var StandDic = {
    id: "dicTree",	//表格id
    seItem: null,		//选中的条目
    table: null,
    ztree: null,
    layerIndex: -1,
    dicMatchId: 0,
    dicId: 0,
    MatchType: 0,
    compare: "",
    PreventUnAC:0,   //阻止反选全部：选择标准字典清除缓存选中医嘱时，需要清空所有选择项。此变量全局实现只反选,不保存。
    dicName: ""

};

StandDic.onClickEntity = function (e, treeId, treeNode) {
    /*if (treeNode.pId == null || treeNode.pId == 0) {
        Sys.info("不能选择根节点（"+treeNode.name+"），请重新选择具体方案。");
        return false;
    }*/
    $("#OrderKey").searchbox("setValue", ""); // 切换字典，清空检索框
    StandDic.MatchType = treeNode.pinyin;
    //默认查询已对照
    StandDic.compare = "match";
    switch(StandDic.MatchType) {
        case '0'	:
            //无需对照
            $('#NullTab').show();
            $('#LisTab').hide();
            $('#EmrTab').hide();
            $('#OrdTab').hide();
            $('#LisItemTab').hide();
            break;
        case '1'	:
            //对照医嘱
            $('#NullTab').hide();
            $('#LisTab').hide();
            $('#EmrTab').hide();
            $('#OrdTab').show();
            $('#LisItemTab').hide();
            StandDic.LoadStandDicMatch(StandDic.MatchType,treeNode.id, StandDic.compare);
            break;
        case '2'	:
            //对照检验医嘱
            $('#NullTab').hide();
            $('#LisTab').show();
            $('#EmrTab').hide();
            $('#OrdTab').hide();
            $('#LisItemTab').hide();
            StandDic.LoadLisDicMatch(StandDic.MatchType,treeNode.id, StandDic.compare);
            break;
        case '3'	:
            //对照病历子标题
            $('#NullTab').hide();
            $('#LisTab').hide();
            $('#EmrTab').show();
            $('#OrdTab').hide();
            $('#LisItemTab').hide();
            StandDic.LoadStandEmrMatch(StandDic.MatchType,treeNode.id, StandDic.compare);
            break;
        case '4'	:
            //对照检验项目
            $('#NullTab').hide();
            $('#LisTab').hide();
            $('#EmrTab').hide();
            $('#OrdTab').hide();
            $('#LisItemTab').show();
            StandDic.LoadLisDicItemMatch(StandDic.MatchType,treeNode.id, StandDic.compare);
            break;
        default:
            break;
    }
    StandDic.dicId = treeNode.id;
    StandDic.dicName= treeNode.name;
};

StandDic.LoadItemMatch = function() {
    switch(StandDic.MatchType) {
        case '0'	:
            //无需对照
            //$('#OrdTab').css('display','block');
            $('#NullTab').show();
            $('#LisTab').hide();
            //$('#RisTab').hide();
            $('#EmrTab').hide();
            $('#OrdTab').hide();
            $('#LisItemTab').hide();
            //StandDic.LoadStandDicMatch(StandDic.MatchType,treeNode.id, StandDic.compare);
            break;
        case '1'	:
            //对照医嘱
            $('#NullTab').hide();
            $('#LisTab').hide();
            $('#EmrTab').hide();
            $('#OrdTab').show();
            $('#LisItemTab').hide();
            StandDic.LoadStandDicMatch(StandDic.MatchType,StandDic.dicId, StandDic.compare);
            break;
        case '2'	:
            //对照检验医嘱
            $('#NullTab').hide();
            $('#LisTab').show();
            $('#EmrTab').hide();
            $('#OrdTab').hide();
            $('#LisItemTab').hide();
            StandDic.LoadLisDicMatch(StandDic.MatchType,StandDic.dicId, StandDic.compare);
            break;
        case '3'	:
            //对照病历子标题
            $('#NullTab').hide();
            $('#LisTab').hide();
            $('#EmrTab').show();
            $('#OrdTab').hide();
            $('#LisItemTab').hide();
            StandDic.LoadStandEmrMatch(StandDic.MatchType,StandDic.dicId, StandDic.compare);
            break;
        case '4'	:
            //对照检验项目
            $('#NullTab').hide();
            $('#LisTab').hide();
            $('#EmrTab').hide();
            $('#OrdTab').hide();
            $('#LisItemTab').show();
            StandDic.LoadLisDicItemMatch(StandDic.MatchType,StandDic.dicId, StandDic.compare);
            break;
        default:
            break;
    }
}

StandDic.LoadStandDicMatch=function(matchType,tId, compare){
    StandDic.gridQCStandDicMatch = $HUI.datagrid("#gridQCStandDicMatch",{
        fit:true,
        pagination: true, //如果为true, 则在DataGrid控件底部显示分页工具栏
        displayMsg: "当前表格显示 {from} 到 {to} ,共 {total} 条记录",
        singleSelect: false,
        //定义是否设置基于该行内容的行高度。设置为 false，则可以提高加载性能
        autoRowHeight: false,
        striped:true,
        rownumbers:true,
        checkOnSelect: false,
        selectOnCheck: true,
        onClickRow: onClickRow,
        loadMsg:'数据加载中...',
        url:"/sds/sdsstanddicmatch/selectOrderMatch",
        method: 'post',
        queryParams:{
            compare: compare,
            matchType: matchType,
            id: tId,
            orderKey: Common_GetValue('OrderKey')
        },
        pageSize: 10,
        pageList : [10,50],
        idField:"id",
        columns:[[
            {field:'checked',checkbox:'true',align:'center',width:30,auto:false},
            {field:'description',title:'医嘱项',width:350,sortable:true},
            {field:'orderTypeDesc',title:'分类',width:300,align:'center'},
        ]]
        ,
        onCheck:function(rindex,rowData){
            if (rowData.checked!=1) StandDic.compareDic();
        },
        onUncheck:function(rindex,rowData){
            if (rowData.checked==1)
            {
                StandDic.undoDic(rowData,0);
            }
        },
        /*onCheckAll:function(rindex,rowData){
            SdsQcentity.compareDic();
        },
        onUncheckAll:function(rindex,rowData){
            //console.log(rindex)
            if (SdsQcentity.PreventUnAC==0){
                SdsQcentity.undoDic(rindex,1);
            }else{
                SdsQcentity.PreventUnAC=0;
            }
        },*/
        onLoadSuccess:function(rowData){
            $('#gridQCStandDicMatch').datagrid('clearChecked');
            $(".datagrid-header-check").html("");
            var data = rowData.rows
            $.each(data, function (idx,val) {
                if (val.checked == 1){
                    $('#gridQCStandDicMatch').datagrid('checkRow', idx);
                }else {
                    $('#gridQCStandDicMatch').datagrid('uncheckRow', idx);
                }
            })
        }
    });

    var editIndex=undefined;
    var modifyBeforeRow = {};
    var modifyAfterRow = {};

    function endEditing(){
        if (editIndex == undefined){return true}
        if ($('#gridQCStandDicMatch').datagrid('validateRow', editIndex)){
            //列表中下拉框实现，修改后把回写feetypename，因为formatter显示的是feetypename字段
            $('#gridQCStandDicMatch').datagrid('endEdit', editIndex);
            modifyAfterRow = $('#gridQCStandDicMatch').datagrid('getRows')[editIndex];
            modifyAfterRow = $('#gridQCStandDicMatch').datagrid('getRows')[editIndex];
            var aStr = JSON.stringify(modifyAfterRow);
            var bStr = JSON.stringify(modifyBeforeRow);
            if(aStr!=bStr){
                //修改默认状态
                var ajax = new $ax(Sys.backendPath + "/cdss/cdssstandardmatch/updateIsDefault", function (data) {
                    //Sys.success("对照成功!");
                    $.messager.popover({msg: '刷新对照信息成功！',type:'success',timeout: 1000});
                    //SdsQcentity.LoadStandDicMatch(SdsQcentity.MatchType,SdsQcentity.cdssRecommendId, SdsQcentity.compare);
                    var tIndex=$('#gridQCStandDicMatch').datagrid('getRowIndex',modifyAfterRow.id);
                    $('#gridQCStandDicMatch').datagrid('updateRow',{
                        index: tIndex,
                        row: {
                            'isDefault':modifyAfterRow.isDefault
                        }
                    });
                }, function (data) {
                    //Sys.error("对照失败!" + data.responseJSON.message + "!");
                    $.messager.alert("错误提示", "数据加载错误!", 'info');
                });
                ajax.set("cdssRecommendId",StandDic.cdssRecommendId);
                ajax.set("orderID",modifyAfterRow.id);
                ajax.set("matchType",StandDic.MatchType);
                ajax.set("isDefault",modifyAfterRow.isDefault);
                ajax.start();
            }
            editIndex = undefined;
            return true;
        } else {
            return false;
        }
    }
    function onClickRow(index){
        if (editIndex!=index) {
            if (endEditing()){
                $('#gridQCStandDicMatch').datagrid('selectRow', index)
                    .datagrid('beginEdit', index);
                editIndex = index;
                modifyBeforeRow = $.extend({},$('#gridQCStandDicMatch').datagrid('getRows')[editIndex]);
            } else {
                $('#dg').datagrid('selectRow', editIndex);
            }
        }
    }

}

StandDic.LoadLisDicMatch=function(matchType,tId, compare){
    StandDic.gridLisOrdMatch = $HUI.datagrid("#gridLisOrdMatch",{
        fit:true,
        pagination: true, //如果为true, 则在DataGrid控件底部显示分页工具栏
        displayMsg: "当前表格显示 {from} 到 {to} ,共 {total} 条记录",
        singleSelect: false,
        //定义是否设置基于该行内容的行高度。设置为 false，则可以提高加载性能
        autoRowHeight: false,
        striped:true,
        rownumbers:true,
        checkOnSelect: false,
        selectOnCheck: true,
        onClickRow: onClickRow,
        loadMsg:'数据加载中...',
        pageSize: 10,
        pageList : [10,50],
        url:"/sds/sdsstanddicmatch/selectLisMatch",
        method: 'post',
        queryParams:{
            compare: compare,
            matchType: matchType,
            id: tId,
            orderKey: Common_GetValue('OrderKey')
        },
        idField:"id",
        columns:[[
            {field:'checked',checkbox:'true',align:'center',width:'30',auto:false},
            {field:'code',title:'代码',width:'100',sortable:true},
            {field:'description',title:'检验医嘱',width:'350'},
            {field:'orderMastDesc',title:'医嘱项',width:'350',sortable:true},
        ]]
        ,
        onCheck:function(rindex,rowData){
            if (rowData.checked!=1) StandDic.compareLis();
        },
        onUncheck:function(rindex,rowData){
            if (rowData.checked==1)
            {
                StandDic.undoLis(rowData,0);
            }

        }/*,
        onCheckAll:function(rindex,rowData){
            SdsQcentity.compareLis();
        },
        onUncheckAll:function(rindex,rowData){
            //console.log(rindex)
            if (SdsQcentity.PreventUnAC==0){
                SdsQcentity.undoLis(rindex,1);
            }else{
                SdsQcentity.PreventUnAC=0;
            }
        }*/
        ,onLoadSuccess:function(rowData){
            $('#gridLisOrdMatch').datagrid('clearChecked');
            $(".datagrid-header-check").html("");
            var data = rowData.rows
            $.each(data, function (idx,val) {
                if (val.checked == 1){
                    $('#gridLisOrdMatch').datagrid('checkRow', idx);
                }else {
                    $('#gridLisOrdMatch').datagrid('uncheckRow', idx);
                }
            })
        }
    });

    var editIndex=undefined;
    var modifyBeforeRow = {};
    var modifyAfterRow = {};

    function endEditing(){
        if (editIndex == undefined){return true}
        if ($('#gridLisOrdMatch').datagrid('validateRow', editIndex)){
            //列表中下拉框实现，修改后把回写feetypename，因为formatter显示的是feetypename字段
            $('#gridLisOrdMatch').datagrid('endEdit', editIndex);
            modifyAfterRow = $('#gridLisOrdMatch').datagrid('getRows')[editIndex];
            modifyAfterRow = $('#gridLisOrdMatch').datagrid('getRows')[editIndex];
            var aStr = JSON.stringify(modifyAfterRow);
            var bStr = JSON.stringify(modifyBeforeRow);
            if(aStr!=bStr){

                //修改默认状态
                var ajax = new $ax(Sys.backendPath + "/cdss/cdssstandardmatch/updateIsDefault", function (data) {
                    //Sys.success("对照成功!");
                    $.messager.popover({msg: '刷新对照信息成功！',type:'success',timeout: 1000});
                    //SdsQcentity.LoadLisDicMatch(SdsQcentity.MatchType,SdsQcentity.cdssRecommendId, SdsQcentity.compare);
                    var tIndex=$('#gridLisOrdMatch').datagrid('getRowIndex',modifyAfterRow.id);
                    $('#gridLisOrdMatch').datagrid('updateRow',{
                        index: tIndex,
                        row: {
                            'isDefault':modifyAfterRow.isDefault
                        }
                    });
                }, function (data) {
                    //Sys.error("对照失败!" + data.responseJSON.message + "!");
                    $.messager.alert("错误提示", "数据加载错误!", 'info');
                });
                ajax.set("cdssRecommendId",StandDic.cdssRecommendId);
                ajax.set("orderID",modifyAfterRow.id);
                ajax.set("matchType",StandDic.MatchType);
                ajax.set("isDefault",modifyAfterRow.isDefault);
                ajax.start();
            }
            editIndex = undefined;
            return true;
        } else {
            return false;
        }
    }
    function onClickRow(index){
        if (editIndex!=index) {
            if (endEditing()){
                $('#gridLisOrdMatch').datagrid('selectRow', index)
                    .datagrid('beginEdit', index);
                editIndex = index;
                modifyBeforeRow = $.extend({},$('#gridLisOrdMatch').datagrid('getRows')[editIndex]);
            } else {
                $('#dg').datagrid('selectRow', editIndex);
            }
        }
    }

}

StandDic.LoadRisDicMatch=function(matchType,tId, compare){
    StandDic.gridRisItemMatch = $HUI.datagrid("#gridRisItemMatch",{
        fit:true,
        pagination: true, //如果为true, 则在DataGrid控件底部显示分页工具栏
        displayMsg: "当前表格显示 {from} 到 {to} ,共 {total} 条记录",
        singleSelect: false,
        //定义是否设置基于该行内容的行高度。设置为 false，则可以提高加载性能
        autoRowHeight: false,
        striped:true,
        rownumbers:true,
        checkOnSelect: false,
        selectOnCheck: true,
        onClickRow: onClickRow,
        loadMsg:'数据加载中...',
        pageSize: 10,
        pageList : [10,50],
        url:"/cdss/cdssstandardmatch/selectRisMatch",
        method: 'post',
        queryParams:{
            compare: compare,
            matchType: matchType,
            id: tId,
            orderKey: Common_GetValue('OrderKey')
        },
        idField:"id",
        columns:[[
            {field:'checked',checkbox:'true',align:'center',width:'30',auto:false},
            {field:'code',title:'检查代码',width:'100',sortable:true},
            {field:'description',title:'检查医嘱',width:'350'},
            {title: '默认', field: 'isDefault', width: 60, align: 'center', valign: 'middle', sortable: true, formatter:function(value,row,index){
                    if(value==1){
                        return "<span class='label label-success'>是</span>";
                    }else{
                        return "<span class='label label-warning'>否</span>";
                    }
                },editor:{
                    type:'combobox',
                    options:{required:true,valueField:'id',textField:'text',editable:false,
                        data:
                            [
                                {'id':1,'text':'是'},
                                {'id':0,'text':'否'}
                            ],
                        onSelect:function (oVal,nVal) {
                            //结束编辑
                            endEditing();
                        }}
                }},

        ]]
        ,
        onCheck:function(rindex,rowData){
            //var chkRows = SdsQcentity.gridRisItemMatch.getChecked();
            //console.log(chkRows)
            if (rowData.checked!=1) StandDic.compareRis();
        },
        onUncheck:function(rindex,rowData){
            //console.log(rowData)
            if (rowData.checked==1)
            {
                StandDic.undoRis(rowData,0);
            }
        },
        /*onCheckAll:function(rindex,rowData){
            SdsQcentity.compareRis();
        },
        onUncheckAll:function(rindex,rowData){
            //console.log(rindex)
            if (SdsQcentity.PreventUnAC==0){
                SdsQcentity.undoRis(rindex,1);
            }else{
                SdsQcentity.PreventUnAC=0;
            }
        },*/
        onLoadSuccess:function(data){
            $('#gridRisItemMatch').datagrid('clearChecked');
            $(".datagrid-header-check").html("");
            var data = data.rows
            $.each(data, function (idx,val) {
                if (val.checked == 1){
                    $('#gridRisItemMatch').datagrid('checkRow', idx);
                }else {
                    $('#gridRisItemMatch').datagrid('uncheckRow', idx);
                }
            })
        }
    });

    var editIndex=undefined;
    var modifyBeforeRow = {};
    var modifyAfterRow = {};

    function endEditing(){
        if (editIndex == undefined){return true}
        if ($('#gridRisItemMatch').datagrid('validateRow', editIndex)){
            //列表中下拉框实现，修改后把回写feetypename，因为formatter显示的是feetypename字段
            $('#gridRisItemMatch').datagrid('endEdit', editIndex);
            modifyAfterRow = $('#gridRisItemMatch').datagrid('getRows')[editIndex];
            modifyAfterRow = $('#gridRisItemMatch').datagrid('getRows')[editIndex];
            var aStr = JSON.stringify(modifyAfterRow);
            var bStr = JSON.stringify(modifyBeforeRow);
            if(aStr!=bStr){
                //修改默认状态
                var ajax = new $ax(Sys.backendPath + "/cdss/cdssstandardmatch/updateIsDefault", function (data) {
                    //Sys.success("对照成功!");
                    $.messager.popover({msg: '刷新对照信息成功！',type:'success',timeout: 1000});
                    //SdsQcentity.LoadRisDicMatch(SdsQcentity.MatchType,SdsQcentity.cdssRecommendId, SdsQcentity.compare);
                    var tIndex=$('#gridRisItemMatch').datagrid('getRowIndex',modifyAfterRow.id);
                    $('#gridRisItemMatch').datagrid('updateRow',{
                        index: tIndex,
                        row: {
                            'isDefault':modifyAfterRow.isDefault
                        }
                    });
                }, function (data) {
                    //Sys.error("对照失败!" + data.responseJSON.message + "!");
                    $.messager.alert("错误提示", "数据加载错误!", 'info');
                });
                ajax.set("cdssRecommendId",StandDic.cdssRecommendId);
                ajax.set("orderID",modifyAfterRow.id);
                ajax.set("matchType",StandDic.MatchType);
                ajax.set("isDefault",modifyAfterRow.isDefault);
                ajax.start();
            }
            editIndex = undefined;
            return true;
        } else {
            return false;
        }
    }
    function onClickRow(index){
        if (editIndex!=index) {
            if (endEditing()){
                $('#gridRisItemMatch').datagrid('selectRow', index)
                    .datagrid('beginEdit', index);
                editIndex = index;
                modifyBeforeRow = $.extend({},$('#gridRisItemMatch').datagrid('getRows')[editIndex]);
            } else {
                $('#dg').datagrid('selectRow', editIndex);
            }
        }
    }

}

StandDic.LoadStandEmrMatch=function(matchType,tId, compare){
    StandDic.gridEmrMatch = $HUI.datagrid("#gridEmrMatch",{
        fit:true,
        pagination: true, //如果为true, 则在DataGrid控件底部显示分页工具栏
        displayMsg: "当前表格显示 {from} 到 {to} ,共 {total} 条记录",
        singleSelect: false,
        //定义是否设置基于该行内容的行高度。设置为 false，则可以提高加载性能
        autoRowHeight: false,
        striped:true,
        rownumbers:true,
        checkOnSelect: false,
        selectOnCheck: true,
        onClickRow: onClickRow,
        autoSave:true,
        loadMsg:'数据加载中...',
        url:"/sds/sdsstanddicmatch/selectEmrMatch",
        method: 'post',
        queryParams:{
            compare: compare,
            matchType: matchType,
            id: tId,
            orderKey: Common_GetValue('OrderKey')
        },
        pageSize: 10,
        pageList : [10,50],
        idField:"id",
        columns:[[
            {field:'checked',checkbox:'true',align:'center',width:30,auto:false},
            {field:'description',title:'描述',width:300,align:'center'},
        ]]
        ,
        onCheck:function(rindex,rowData){
            if (rowData.checked!=1) StandDic.compareEmr();
        },
        onUncheck:function(rindex,rowData){
            if (rowData.checked==1)
            {
                StandDic.undoEmr(rowData,0);
            }
        },
        /*onCheckAll:function(rindex,rowData){
            SdsQcentity.compareEval();
        },
        onUncheckAll:function(rindex,rowData){
            //console.log(rindex)
            if (SdsQcentity.PreventUnAC==0){
                SdsQcentity.undoEval(rindex,1);
            }else{
                SdsQcentity.PreventUnAC=0;
            }
        }
        ,*/
        onLoadSuccess:function(rowData){
            $('#gridEmrMatch').datagrid('clearChecked');
            $(".datagrid-header-check").html("");
            var data = rowData.rows
            $.each(data, function (idx,val) {
                if (val.checked == 1){
                    $('#gridEmrMatch').datagrid('checkRow', idx);
                }else {
                    $('#gridEmrMatch').datagrid('uncheckRow', idx);
                }
            })
        }
    });

    var editIndex=undefined;
    var modifyBeforeRow = {};
    var modifyAfterRow = {};

    function endEditing(){
        if (editIndex == undefined){return true}
        if ($('#gridEmrMatch').datagrid('validateRow', editIndex)){
            //列表中下拉框实现，修改后把回写feetypename，因为formatter显示的是feetypename字段
            $('#gridEmrMatch').datagrid('endEdit', editIndex);
            modifyAfterRow = $('#gridEmrMatch').datagrid('getRows')[editIndex];
            modifyAfterRow = $('#gridEmrMatch').datagrid('getRows')[editIndex];
            var aStr = JSON.stringify(modifyAfterRow);
            var bStr = JSON.stringify(modifyBeforeRow);
            if(aStr!=bStr){
                //修改默认状态
                var ajax = new $ax(Sys.backendPath + "/cdss/cdssstandardmatch/updateIsDefault", function (data) {
                    //Sys.success("对照成功!");
                    $.messager.popover({msg: '刷新对照信息成功！',type:'success',timeout: 1000});
                    //SdsQcentity.LoadStandEvalMatch(SdsQcentity.MatchType,SdsQcentity.cdssRecommendId, SdsQcentity.compare);
                    var tIndex=$('#gridEmrMatch').datagrid('getRowIndex',modifyAfterRow.id);
                    $('#gridEmrMatch').datagrid('updateRow',{
                        index: tIndex,
                        row: {
                            'isDefault':modifyAfterRow.isDefault
                        }
                    });
                }, function (data) {
                    //Sys.error("对照失败!" + data.responseJSON.message + "!");
                    $.messager.alert("错误提示", "数据加载错误!", 'info');
                });
                ajax.set("cdssRecommendId",StandDic.cdssRecommendId);
                ajax.set("orderID",modifyAfterRow.id);
                ajax.set("matchType",StandDic.MatchType);
                ajax.set("isDefault",modifyAfterRow.isDefault);
                ajax.start();
            }
            editIndex = undefined;
            return true;
        } else {
            return false;
        }
    }
    function onClickRow(index){
        if (editIndex!=index) {
            if (endEditing()){
                $('#gridEmrMatch').datagrid('selectRow', index)
                    .datagrid('beginEdit', index);
                editIndex = index;
                modifyBeforeRow = $.extend({},$('#gridEmrMatch').datagrid('getRows')[editIndex]);
            } else {
                $('#dg').datagrid('selectRow', editIndex);
            }
        }
    }
}

StandDic.LoadLisDicItemMatch=function(matchType,tId, compare){
    StandDic.gridLisItemMatch = $HUI.datagrid("#gridLisItemMatch",{
        fit:true,
        pagination: true, //如果为true, 则在DataGrid控件底部显示分页工具栏
        displayMsg: "当前表格显示 {from} 到 {to} ,共 {total} 条记录",
        singleSelect: false,
        //定义是否设置基于该行内容的行高度。设置为 false，则可以提高加载性能
        autoRowHeight: false,
        striped:true,
        rownumbers:true,
        checkOnSelect: false,
        selectOnCheck: true,
        onClickRow: onClickRow,
        loadMsg:'数据加载中...',
        url:"/sds/sdsstanddicmatch/selectLisItemMatch",
        method: 'post',
        queryParams:{
            compare: compare,
            matchType: matchType,
            id: tId,
            orderKey: Common_GetValue('OrderKey')
        },
        pageSize: 10,
        pageList : [10,50],
        idField:"id",
        columns:[[
            {field:'checked',checkbox:'true',align:'center',width:30,auto:false},
            {field:'code',title:'代码',width:350,sortable:true},
            {field:'description',title:'检验项目',width:300,align:'center'},
            {field:'labSetDesc',title:'所属检验医嘱',width:300,align:'center'},
        ]]
        ,
        onCheck:function(rindex,rowData){
            if (rowData.checked!=1) StandDic.compareLisItem();
        },
        onUncheck:function(rindex,rowData){
            if (rowData.checked==1)
            {
                StandDic.undoLisItem(rowData,0);
            }
        },
        /*onCheckAll:function(rindex,rowData){
            SdsQcentity.compareDic();
        },
        onUncheckAll:function(rindex,rowData){
            //console.log(rindex)
            if (SdsQcentity.PreventUnAC==0){
                SdsQcentity.undoDic(rindex,1);
            }else{
                SdsQcentity.PreventUnAC=0;
            }
        },*/
        onLoadSuccess:function(rowData){
            $('#gridLisItemMatch').datagrid('clearChecked');
            $(".datagrid-header-check").html("");
            var data = rowData.rows
            $.each(data, function (idx,val) {
                if (val.checked == 1){
                    $('#gridLisItemMatch').datagrid('checkRow', idx);
                }else {
                    $('#gridLisItemMatch').datagrid('uncheckRow', idx);
                }
            })
        }
    });

    var editIndex=undefined;
    var modifyBeforeRow = {};
    var modifyAfterRow = {};

    function endEditing(){
        if (editIndex == undefined){return true}
        if ($('#gridLisItemMatch').datagrid('validateRow', editIndex)){
            //列表中下拉框实现，修改后把回写feetypename，因为formatter显示的是feetypename字段
            $('#gridLisItemMatch').datagrid('endEdit', editIndex);
            modifyAfterRow = $('#gridLisItemMatch').datagrid('getRows')[editIndex];
            modifyAfterRow = $('#gridLisItemMatch').datagrid('getRows')[editIndex];
            var aStr = JSON.stringify(modifyAfterRow);
            var bStr = JSON.stringify(modifyBeforeRow);
            if(aStr!=bStr){
                //修改默认状态
                var ajax = new $ax(Sys.backendPath + "/cdss/cdssstandardmatch/updateIsDefault", function (data) {
                    //Sys.success("对照成功!");
                    $.messager.popover({msg: '刷新对照信息成功！',type:'success',timeout: 1000});
                    //SdsQcentity.LoadStandDicMatch(SdsQcentity.MatchType,SdsQcentity.cdssRecommendId, SdsQcentity.compare);
                    var tIndex=$('#gridLisItemMatch').datagrid('getRowIndex',modifyAfterRow.id);
                    $('#gridLisItemMatch').datagrid('updateRow',{
                        index: tIndex,
                        row: {
                            'isDefault':modifyAfterRow.isDefault
                        }
                    });
                }, function (data) {
                    //Sys.error("对照失败!" + data.responseJSON.message + "!");
                    $.messager.alert("错误提示", "数据加载错误!", 'info');
                });
                ajax.set("cdssRecommendId",StandDic.cdssRecommendId);
                ajax.set("orderID",modifyAfterRow.id);
                ajax.set("matchType",StandDic.MatchType);
                ajax.set("isDefault",modifyAfterRow.isDefault);
                ajax.start();
            }
            editIndex = undefined;
            return true;
        } else {
            return false;
        }
    }
    function onClickRow(index){
        if (editIndex!=index) {
            if (endEditing()){
                $('#gridLisItemMatch').datagrid('selectRow', index)
                    .datagrid('beginEdit', index);
                editIndex = index;
                modifyBeforeRow = $.extend({},$('#gridLisItemMatch').datagrid('getRows')[editIndex]);
            } else {
                $('#dg').datagrid('selectRow', editIndex);
            }
        }
    }

}

StandDic.compareRis = function(){
    var chkRows = StandDic.gridRisItemMatch.getChecked();
    var orderIDStr = ""
    for (var i=0;i<chkRows.length;i++) {
        var tmpRow = chkRows[i]
        var orderID = tmpRow.id
        orderIDStr += orderID+","
    }

    var ajax = new $ax(Sys.backendPath + "/cdss/cdssstandardmatch/compare", function (data) {
        //Sys.success("对照成功!");
        $.messager.popover({msg: '刷新对照信息成功！',type:'success',timeout: 1000});
        StandDic.LoadRisDicMatch(StandDic.MatchType,StandDic.cdssRecommendId, StandDic.compare);
    }, function (data) {
        //Sys.error("对照失败!" + data.responseJSON.message + "!");
        $.messager.alert("错误提示", "数据加载错误!", 'info');
    });
    ajax.set("cdssRecommendId",StandDic.cdssRecommendId);
    ajax.set("orderIDStr",orderIDStr);
    ajax.set("matchType",StandDic.MatchType);
    ajax.start();
}

StandDic.undoRis = function(chkRows,flag){
    var orderIDStr = ""
    if (flag == 0){
        orderIDStr = chkRows.id+","
    }else {
        for (var i=0;i<chkRows.length;i++) {
            var tmpRow = chkRows[i]
            var orderID = tmpRow.id
            orderIDStr += orderID+","
        }
    }

    var ajax = new $ax(Sys.backendPath + "/cdss/cdssstandardmatch/undo", function (data) {
        //Sys.success("对照成功!");
        $.messager.popover({msg: '刷新对照信息成功！',type:'success',timeout: 1000});
        StandDic.LoadRisDicMatch(StandDic.MatchType,StandDic.cdssRecommendId, StandDic.compare);
    }, function (data) {
        //Sys.error("对照失败!" + data.responseJSON.message + "!");
        $.messager.alert("错误提示", "数据加载错误!", 'info');
    });
    ajax.set("cdssRecommendId",StandDic.cdssRecommendId);
    ajax.set("orderIDStr",orderIDStr);
    ajax.set("matchType",StandDic.MatchType);
    ajax.start();
}

StandDic.compareLis = function(){
    var chkRows = StandDic.gridLisOrdMatch.getChecked();
    var orderIDStr = ""
    for (var i=0;i<chkRows.length;i++) {
        var tmpRow = chkRows[i]
        var orderID = tmpRow.id
        orderIDStr += orderID+","
    }

    var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddicmatch/compare", function (data) {
        //Sys.success("对照成功!");
        $.messager.popover({msg: '刷新对照信息成功！',type:'success',timeout: 1000});
        StandDic.LoadLisDicMatch(StandDic.MatchType,StandDic.dicId, StandDic.compare);
    }, function (data) {
        //Sys.error("对照失败!" + data.responseJSON.message + "!");
        $.messager.alert("错误提示", "数据加载错误!", 'info');
    });
    ajax.set("dicId",StandDic.dicId);
    ajax.set("orderIDStr",orderIDStr);
    ajax.set("matchType",StandDic.MatchType);
    ajax.start();
}

StandDic.undoLis = function(chkRows,flag){
    var orderIDStr = ""
    if (flag == 0){
        orderIDStr = chkRows.id+","
    }else {
        for (var i=0;i<chkRows.length;i++) {
            var tmpRow = chkRows[i]
            var orderID = tmpRow.id
            orderIDStr += orderID+","
        }
    }

    var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddicmatch/undo", function (data) {
        //Sys.success("对照成功!");
        $.messager.popover({msg: '刷新对照信息成功！',type:'success',timeout: 1000});
        StandDic.LoadLisDicMatch(StandDic.MatchType,StandDic.dicId, StandDic.compare);
    }, function (data) {
        //Sys.error("对照失败!" + data.responseJSON.message + "!");
        $.messager.alert("错误提示", "数据加载错误!", 'info');
    });
    ajax.set("dicId",StandDic.dicId);
    ajax.set("orderIDStr",orderIDStr);
    ajax.set("matchType",StandDic.MatchType);
    ajax.start();
}

StandDic.compareDic = function(){
    var chkRows = StandDic.gridQCStandDicMatch.getChecked();
    var orderIDStr = ""
    for (var i=0;i<chkRows.length;i++) {
        var tmpRow = chkRows[i]
        var orderID = tmpRow.id
        orderIDStr += orderID+","
    }

    var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddicmatch/compare", function (data) {
        //Sys.success("对照成功!");
        $.messager.popover({msg: '刷新对照信息成功！',type:'success',timeout: 1000});
        StandDic.LoadStandDicMatch(StandDic.MatchType,StandDic.dicId, StandDic.compare);
    }, function (data) {
        //Sys.error("对照失败!" + data.responseJSON.message + "!");
        $.messager.alert("错误提示", "数据加载错误!", 'info');
    });
    ajax.set("dicId",StandDic.dicId);
    ajax.set("orderIDStr",orderIDStr);
    ajax.set("matchType",StandDic.MatchType);
    ajax.start();
}

StandDic.undoDic = function(chkRows,flag){
    var orderIDStr = ""
    if (flag == 0){
        orderIDStr = chkRows.id+","
    }else {
        for (var i=0;i<chkRows.length;i++) {
            var tmpRow = chkRows[i]
            var orderID = tmpRow.id
            orderIDStr += orderID+","
        }
    }

    var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddicmatch/undo", function (data) {
        //Sys.success("对照成功!");
        $.messager.popover({msg: '刷新对照信息成功！',type:'success',timeout: 1000});
        StandDic.LoadStandDicMatch(StandDic.MatchType,StandDic.dicId, StandDic.compare);
    }, function (data) {
        //Sys.error("对照失败!" + data.responseJSON.message + "!");
        $.messager.alert("错误提示", "数据加载错误!", 'info');
    });
    ajax.set("dicId",StandDic.dicId);
    ajax.set("orderIDStr",orderIDStr);
    ajax.set("matchType",StandDic.MatchType);
    ajax.start();
}

StandDic.compareEmr = function(){
    var chkRows = StandDic.gridEmrMatch.getChecked();
    var orderIDStr = ""
    for (var i=0;i<chkRows.length;i++) {
        var tmpRow = chkRows[i]
        var orderID = tmpRow.description
        orderIDStr += orderID+","
    }

    var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddicmatch/compare", function (data) {
        //Sys.success("对照成功!");
        $.messager.popover({msg: '刷新对照信息成功！',type:'success',timeout: 1000});
        StandDic.LoadStandEmrMatch(StandDic.MatchType,StandDic.dicId, StandDic.compare);
    }, function (data) {
        //Sys.error("对照失败!" + data.responseJSON.message + "!");
        $.messager.alert("错误提示", "数据加载错误!", 'info');
    });
    ajax.set("dicId",StandDic.dicId);
    ajax.set("orderIDStr",orderIDStr);
    ajax.set("matchType",StandDic.MatchType);
    ajax.start();
}

StandDic.undoEmr = function(chkRows,flag){
    var orderIDStr = ""
    if (flag == 0){
        orderIDStr = chkRows.description+","
    }else {
        for (var i=0;i<chkRows.length;i++) {
            var tmpRow = chkRows[i]
            var orderID = tmpRow.description
            orderIDStr += orderID+","
        }
    }

    var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddicmatch/undo", function (data) {
        //Sys.success("对照成功!");
        $.messager.popover({msg: '刷新对照信息成功！',type:'success',timeout: 1000});
        StandDic.LoadStandEmrMatch(StandDic.MatchType,StandDic.dicId, StandDic.compare);
    }, function (data) {
        //Sys.error("对照失败!" + data.responseJSON.message + "!");
        $.messager.alert("错误提示", "数据加载错误!", 'info');
    });
    ajax.set("dicId",StandDic.dicId);
    ajax.set("orderIDStr",orderIDStr);
    ajax.set("matchType",StandDic.MatchType);
    ajax.start();
}

StandDic.compareLisItem = function(){
    var chkRows = StandDic.gridLisItemMatch.getChecked();
    var labItemIDStr = ""
    for (var i=0;i<chkRows.length;i++) {
        var tmpRow = chkRows[i]
        var labItemID = tmpRow.id
        labItemIDStr += labItemID+","
    }

    var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddicmatch/compare", function (data) {
        //Sys.success("对照成功!");
        $.messager.popover({msg: '刷新对照信息成功！',type:'success',timeout: 1000});
        StandDic.LoadLisDicItemMatch(StandDic.MatchType,StandDic.dicId, StandDic.compare);
    }, function (data) {
        //Sys.error("对照失败!" + data.responseJSON.message + "!");
        $.messager.alert("错误提示", "数据加载错误!", 'info');
    });
    ajax.set("dicId",StandDic.dicId);
    ajax.set("orderIDStr",labItemIDStr);
    ajax.set("matchType",StandDic.MatchType);
    ajax.start();
}

StandDic.undoLisItem = function(chkRows,flag){
    var orderIDStr = ""
    if (flag == 0){
        orderIDStr = chkRows.id+","
    }else {
        for (var i=0;i<chkRows.length;i++) {
            var tmpRow = chkRows[i]
            var orderID = tmpRow.id
            orderIDStr += orderID+","
        }
    }

    var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddicmatch/undo", function (data) {
        //Sys.success("对照成功!");
        $.messager.popover({msg: '刷新对照信息成功！',type:'success',timeout: 1000});
        StandDic.LoadLisDicItemMatch(StandDic.MatchType,StandDic.dicId, StandDic.compare);
    }, function (data) {
        //Sys.error("对照失败!" + data.responseJSON.message + "!");
        $.messager.alert("错误提示", "数据加载错误!", 'info');
    });
    ajax.set("dicId",StandDic.dicId);
    ajax.set("orderIDStr",orderIDStr);
    ajax.set("matchType",StandDic.MatchType);
    ajax.start();
}

function getCurrentRoot(treeNode){
    if(treeNode.getParentNode() != null){
        var parentNode = treeNode.getParentNode();
        return getCurrentRoot(parentNode);
    }else {
        return treeNode.id;
    }
}

//取值
function Common_GetValue() {
    var itmValue = '';

    if (typeof(arguments[0]) !== 'string') return '';
    if (arguments[0] == '') return '';

    var $this = $('#'+ arguments[0]);
    if ($this.length < 1) return '';

    var className = $this.attr("class").split(' ')[0];

    if (className == 'textbox') {  //文本框
        itmValue = $this.val();
    }else if (className == 'hisui-numberbox') {  //数字
        itmValue = $this.val();
    }else if (className == 'hisui-datebox') {  //日期
        itmValue = $this.datebox('getValue');
    }else if (className == 'hisui-timespinner') {  //时间框
        itmValue = $this.timespinner('getValue');
    }else if (className == 'hisui-combobox') {  //下拉框（多选下拉框没有封装）
        itmValue = $this.combobox('getValue');
    }else if (className == 'hisui-switchbox') {  //开关
        itmValue = $this.switchbox('getValue');
    }else if (className == 'hisui-checkbox') {  // 单个复选框
        itmValue = $this.checkbox('getValue');
    }else if (className == 'hisui-radio') {  //单个单选框
        itmValue = $this.radio('getValue');
    }else if (className == 'hisui-searchbox') {  //查询框框
        itmValue = $this.searchbox('getValue');
    }

    return itmValue;
}

/**
 * 病种筛查配置管理初始化
 */
var SdsQcentityRule = {
    id: "SdsQcentityRuleTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

function pagerFilter(data) {
    if (typeof data.length == 'number' && typeof data.splice == 'function') {// is array
        data = {
            total: data.length,
            rows: data
        }
    }
    var dg = $(this);
    var opts = dg.datagrid('options');
    var pager = dg.datagrid('getPager');
    pager.pagination({
        onSelectPage: function (pageNum, pageSize) {
            opts.pageNumber = pageNum;
            opts.pageSize = pageSize;
            pager.pagination('refresh', {pageNumber: pageNum, pageSize: pageSize});
            dg.datagrid('loadData', data);
        }
    });
    if (!data.originalRows) {
        data.originalRows = (data.rows);
    }
    var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
    var end = start + parseInt(opts.pageSize);
    data.rows = (data.originalRows.slice(start, end));
    return data;
}


/**
 * 检查是否选中病种
 */
SdsQcentityRule.check = function () {
    if (SdsQcentity.cdssRecommendId) {
        return true;
    } else {
        Sys.info("请先选中某一方案！");
        return false;
    }
};

/**
 * 验证数据是否为空
 */
SdsQcentityRule.validate = function () {
    var errinfo = "";
    if ($("#param2").val()=="") {
        errinfo = errinfo + "请填写关键字再保存!<br>";
    }
    if (errinfo) {
        $.messager.alert("错误提示", errinfo, 'info');
        return false;
    }
    return true;
}

/**
 * 自动匹配
 */
SdsQcentityRule.autoCompare = function (){
    if ("" != StandDic.dicName){
        //对照选中节点及其子节点
        var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddicmatch/autoCompare", function (data) {
                Sys.success("自动匹配成功!");
                StandDic.LoadItemMatch();
            }, function (data) {
                Sys.error("自动匹配失败!" + data.responseJSON.message + "!");
            });
            ajax.set("dicId",StandDic.dicId);
            ajax.set("dicName",StandDic.dicName);
            ajax.set("matchType",StandDic.MatchType);
            ajax.start();
        };
        Sys.confirm("是否对" + StandDic.dicName + "及其子节点进行自动匹配?",operation);
    }
}

SdsQcentityRule.autoCompareAll = function (){
    //对照全部
    var operation = function(){
            var ajax = new $ax(Sys.backendPath + "/sds/sdsstanddicmatch/autoCompare", function (data) {
                Sys.success("自动匹配成功!");
                StandDic.LoadItemMatch();
            }, function (data) {
                Sys.error("自动匹配失败!" + data.responseJSON.message + "!");
            });
            ajax.set("dicId","");
            ajax.set("dicName","");
            ajax.set("matchType","");
            ajax.start();
        };
    Sys.confirm("是否对全部字典进行自动匹配?",operation);
}

SdsQcentityRule.compareAll = function (){
    StandDic.compare = "all";
    StandDic.LoadItemMatch();
}

SdsQcentityRule.compareNo = function (){
    StandDic.compare = "noMatch";
    StandDic.LoadItemMatch();
}

SdsQcentityRule.compareYes = function (){
    StandDic.compare = "match";
    StandDic.LoadItemMatch();
}


/**
 * 查询病种筛查配置列表
 */
SdsQcentityRule.search = function () {
    var queryData = {};
    queryData['entityId'] = SdsQcentity.entityId;
    //SdsQcentityRule.table.refresh({query: queryData, pageNumber: 1});
};

$(function () {
    //查询病种树
    var ztree = new $ZTree("dicTree", Sys.backendPath + "/sds/sdsstanddic/tree");
    ztree.bindOnClick(StandDic.onClickEntity);
    ztree.init();
    StandDic.ztree = ztree;
    fuzzySearch('dicTree','#seachKey',null,true);

    $('#OrderKey').searchbox({
        searcher:function(value){
            StandDic.LoadItemMatch();
        },
        prompt:'医嘱关键字'
    });

    //项目规则获取
    /*var defaultColunms = SdsQcentityRule.initColumn();
    var table = new BSTable(SdsQcentityRule.id, Sys.backendPath + "/sds/sdsqcentityrule/list", defaultColunms);
    table.setPaginationType("server");
    table.setPageSize(10);
    table.setSubHeight(-40);
    table.setShowExport(false);
    table.setShowRefresh(false);
    table.setShowToggle(false);
    table.setShowColumns(false);
    table.setOnClickRow(SdsQcentityRule.fillData);
    table.setQueryParams({"entityId":0});
    SdsQcentityRule.table = table.init();*/
});
